<?php

class image_tasks_imagerebuild extends base_task_abstract implements base_interface_task {

    public function exec($params = null) {
        logger::info('image rebuild start with $params: ' . json_encode($params));
        // 每次最多处理2个
        $limit = 2;
        $model = app::get('image')->model('image');
        $db = kernel::database();
        if ($params['filter']['image_id'] == '_ALL_' || $params['filter']['image_id'] == '_ALL_') {
            unset($params['filter']['image_id']);
        }
        $where = $model->_filter($params['filter']);
        $where .= ' and last_modified<=' . $params['queue_time'];
        $rows = $db->select('select image_id from ' . DB_PREFIX . 'image_image where ' . $where . ' order by last_modified desc limit ' . $limit);
        logger::info('image rebuild with images $rows : ' . json_encode($rows));
        foreach ($rows as $r) {
            if ($params['watermark'] == 'false') {
                $params['watermark'] = false;
            }
            $model->rebuild($r['image_id'], $params['size'], $params['watermark']);
        }
        $r = $db->selectrow('select count(*) as c from ' . DB_PREFIX . 'image_image where ' . $where);
        
        logger::info('image rebuild result $r : ' . json_encode($r));
        return $r['c'];
    }
}

